package com.itgaoshu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.bean.SysUser;
import com.itgaoshu.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: xiyue
 * Date: 2021/3/19
 * Time: 17:26
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Resource
    private SysUserService  userService;

    @RequestMapping("selectAllUser")
    @ResponseBody
    public Object selectAllUser(Integer page,Integer limit,SysUser user){
        PageHelper.startPage(page, limit);
        List<SysUser> allUser = userService.selectAllUsers();
        PageInfo pageInfo = new PageInfo(allUser);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        dataMap.put("code", 0);
        dataMap.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        dataMap.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        dataMap.put("data", pageInfo.getList());

        return dataMap;
    }


    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("addUser")
    @ResponseBody
    public Object addUser(SysUser user){
        int i1 = userService.checkUser((String)user.getLoginname());
        if(i1==1){
            return "当前登陆名已存在";
        }else {
            user.setType(2);
            int i = userService.addUser(user);
            if (i > 0) {
                return "添加成功";
            } else {
                return "添加失败";
            }
        }
    }

    //删除用户
    @RequestMapping("deleteUser")
    @ResponseBody
    public Object deleteUser(Integer userid){
        int result = userService.deleteUserById(userid);
        if(result>0) {
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    //修改用户
    /*
     * 修改资料
     * */
    @RequestMapping("updateUser")
    @ResponseBody
    public Object editLogin(SysUser user){
        System.out.println(user);
        int i = userService.updateUser(user);
        if(i==1){
            return "修改成功";
        }else{
            return "修改失败";
        }

    }
}
